package com.cmwa.scbp.business.postMaintenance.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.scbp.base.mvc.controller.BaseController;
import com.cmwa.scbp.base.mvc.entity.Page;
import com.cmwa.scbp.base.mvc.entity.Result;
import com.cmwa.scbp.base.mvc.entity.SearchParam;
import com.cmwa.scbp.business.postMaintenance.entity.PostPersonnelChangeVo;
import com.cmwa.scbp.business.postMaintenance.service.PostPersonnelChangeService;
import com.cmwa.scbp.sys.menu.service.MenuService;
import com.cmwa.scbp.sys.permission.post.entity.PostVo;
import com.cmwa.scbp.sys.permission.post.service.PostService;
import com.cmwa.scbp.utils.SessionUtils;

import net.sf.json.JSONArray;

/**
 * 产品与岗位/人员维护控制层
 * 
 * @author ex-sujh
 * @date 2019-02-22 10:42:18
 */
@Controller
@RequestMapping("/business/postMaintenance")
public class PostMaintenanceController extends BaseController {

	private final static String POST_MAINTENANCE_PATH = "business/postMaintenance/";
	@Resource
	private PostPersonnelChangeService postPersonnelChangeService;
	@Resource
	private PostService postService;
	@Resource
	private MenuService menuService;
	
	
	/**
	 * 跳转到批量变更列表页面
	 * @author ex-sujh
	 * @date 2019-02-22 10:51:57
	 * @param model
	 * @return
	 */
	@RequestMapping("/postPersonnelChangeListView")
	public String postPersonnelChangeListView(ModelMap model) {
		List<PostVo> postList = postService.queryIsConfPostList();
		List<String> codes = menuService.queryMcodeByEmpIdAndMcode(SessionUtils.getEmployee().getID(), "post_%_edit");
		model.addAttribute("postList", postList);
		model.addAttribute("codes", JSONArray.fromObject(codes));
		return POST_MAINTENANCE_PATH + "postPersonnelChangeList";
	}
	
	
	@RequestMapping("/postPersonnelChangeListPage")
	@ResponseBody
	public Page<PostPersonnelChangeVo> postPersonnelChangeListPage(SearchParam param) {
		Page<PostPersonnelChangeVo> list = postPersonnelChangeService.postPersonnelChangeListPage(param);
		return list;
	}
	
	/**
	 * 跳转岗位变更新增页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/postPersonnelChangeAddView")
	public String postPersonnelChangeAddView(Model model) {
		List<PostVo> postList = postService.queryIsConfPostList();
		model.addAttribute("postList", postList);
		return POST_MAINTENANCE_PATH + "postPersonnelChangeAdd";
	}
	
	/**
	 * 查询对应层级的资源
	 * @param sp
	 * @return
	 */
	@RequestMapping("/queryResouceListByCondition")
	@ResponseBody
	public List<Map<String, String>> queryResouceListByCondition(SearchParam sp) {
		return postPersonnelChangeService.queryResouceListByCondition(sp);
	}
	
	/**
	 * 保存变更
	 * @param postPersonnelChangeVo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/savePostPersonnelChange")
	public String savePostPersonnelChange(PostPersonnelChangeVo postPersonnelChangeVo) throws Exception {
		postPersonnelChangeService.savePostPersonnelChange(postPersonnelChangeVo);
		return redirectSuccess();
	}
	
	/**
	 * 校验生效日唯一
	 * @param postPersonnelChangeVo
	 * @return
	 */
	@RequestMapping("/checkEffctiveDateUnique")
	@ResponseBody
	public Result<?> checkEffctiveDateUnique(PostPersonnelChangeVo postPersonnelChangeVo) {
		Result<?> reuslt = postPersonnelChangeService.checkEffctiveDateUnique(postPersonnelChangeVo);
		return reuslt;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/deletePostPersonnelChange")
	@ResponseBody
	public Result<?> deletePostPersonnelChange(@RequestParam("id") String id) throws Exception {
		Result<?> result = new Result<>();
		postPersonnelChangeService.deletePostPersonnelChange(id);
		result.setSuccess(true);
		result.setMessage("删除成功");
		return result;
	}
}
